﻿using System;
using System.Linq;
using Solution.BusinessObject;
using Solution.CMS.MainDSTableAdapters;
using Solution.Core;
using Solution.Utilities;

namespace Solution.CMS
{
    public partial class AlbumsEditor : PageBase
    {
        private readonly MainDS _mainDs = new MainDS();
        private MainDS.ContentsRow _contentRow;

        private readonly ContentsTableAdapter _contentTableAdapter =
            new ContentsTableAdapter();

        private readonly CategoriesTableAdapter _categoriesTableAdapter =
            new CategoriesTableAdapter();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }
            _categoriesTableAdapter.Connection = CommonBO.SqlConnection;
            _categoriesTableAdapter.Connection.Open();
            _contentTableAdapter.Connection = CommonBO.SqlConnection;
            _contentTableAdapter.Connection.Open();

            _categoriesTableAdapter.Fill(_mainDs.Categories);
            _contentTableAdapter.Fill(_mainDs.Contents);

            cboParent.DataSource =
                _mainDs
                    .Categories.Rows
                    .Cast<MainDS.CategoriesRow>()
                    .Where(row => row.CategoriesRowParent != null).ToList();
            cboParent.DataBind();

            // new
            if (Request["id"] == null)
            {
                btnSave.PostBackUrl = "albumseditor.aspx";
            }
            // edit
            else
            {
                var id = Request["id"];

                _contentRow = _mainDs.Contents.FindById(Convert.ToInt32(id));
                if (_contentRow != null)
                {
                    txtTitle.Text = _contentRow.Title;
                    txtThumbnail.Text = _contentRow.Thumb;
                    txtIntro.Text = _contentRow.Intro;
                    cboParent.Value = _contentRow.Category_Id;
                }
                btnSave.PostBackUrl = "albumseditor.aspx?id=" + id;
            }

            _contentTableAdapter.Connection.Close();

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                _contentTableAdapter.Connection = CommonBO.SqlConnection;
                _contentTableAdapter.Connection.Open();

                _contentTableAdapter.Fill(_mainDs.Contents);

                // new
                if (Request["id"] == null)
                {
                    _contentRow = (MainDS.ContentsRow)_mainDs.Contents.NewRow();
                    _mainDs.Contents.Rows.Add(_contentRow);
                }
                // edit
                else
                {
                    var id = Request["id"];

                    _contentRow = _mainDs.Contents.FindById(Convert.ToInt32(id));
                }

                _contentRow.Title = txtTitle.Text;
                _contentRow.Thumb = txtThumbnail.Text;
                _contentRow.Intro = txtIntro.Text;
                if (cboParent.Value !=null)
                {
                    _contentRow.Category_Id = (int)cboParent.Value;
                }
                else
                {
                    _contentRow.SetCategory_IdNull();
                }

                _contentTableAdapter.Adapter.Update(_mainDs);
                _contentTableAdapter.Connection.Close();
                
                Response.Redirect("albums.aspx");
            }
            catch (Exception exception)
            {
                Logger.WriteLog(exception);
            }
        }
    }
}